#include "twap.h"
#include <iostream>
using namespace std;

twap::twap (const std::string& _name, const std::string& _symbol, int hs, int ms, int ss, int he, int me, int se, int _exec, int _num_shares, int _total_trades, double _finish_price, double _stop_price) 
: bot (_name, _symbol, hs, ms, ss, he, me, se, _exec, _num_shares, _finish_price, _stop_price), viewer ("twap.ed")
{
  
  total_trades = _total_trades;
  
  double d_trade_size = num_shares * 1. / total_trades;
  
  trade_size = (int) d_trade_size; 
  
  size_error = d_trade_size - trade_size;
  cur_size_error = 0;
  
  trade_interval = avail_time * 1. / total_trades;
  
  TWAP = 0;
  
  pviewer = &viewer;
  
}

twap::~twap () {
  viewer.save ();
}

int twap::run (double secs) {
  
  if (bot::run (secs)) {
    cout << "TWAP = " << (value * 1. / position) << endl;
  }
  
  return 1;
  
}

int twap::get_trade_size (double t) {
  int delta_size = 0;
  cur_size_error += size_error;
  if (cur_size_error >= 1) {
    cur_size_error -= 1;
    delta_size = 1;
  }
  cout << "size err: " << cur_size_error << endl;
  return (trade_size + delta_size);  
}

double twap::get_trade_interval () {
  return trade_interval;
}
